﻿@model ProductModel
<script type="text/javascript">
    $(document).ready(function () {
    toastr.options = {
                            closeButton: false,
                            debug: false,
                            progressBar: false,
                            positionClass: "toast-top-center",
                            onclick: null,
                            showDuration: "300",
                            hideDuration: "1000",
                            // timeOut: "1000",
                            extendedTimeOut: "1000",
                            showEasing: "swing",
                            hideEasing: "linear",
                            showMethod: "fadeIn",
                            hideMethod: "fadeOut"
                        };
    var productId = '@Model.Id';
    var price = '@Model.Price';
    $.ajax({
        cache: false,
        url: '@Html.Raw(Url.Action("WalletTemplate", "Product"))',
        success: function (data) {
            var templates = data;
            templates.sort((item) => {
                return item.Default;
            });
            var templateDefalt = templates[0];
            var calcTypes = [{ name: "百分比", CalcType: 1 }, { name: "绝对值", CalcType: 2 }];
            var calcDefalt = calcTypes[0];
            var rowId = kendo.guid();

            function walletDropDownChange() {
                var disable = templateDefalt.CanDeposit;
                if (!disable) {
                    $("#" + rowId + "_auto_deposit_checkbox").attr("disabled", "disabled");
                    $("#" + rowId + "_auto_deposit_checkbox").removeAttr("checked");

                }
                else {
                    $("#" + rowId + "_auto_deposit_checkbox").removeAttr("disabled"); 
                }
            }

            function calcDropDownChange() {
                var format = calcDefalt.CalcType==1?"#.％":"#.";
                $("#" + rowId + "_maxvalue_textbox").kendoNumericTextBox({ format:format });
                $("#" + rowId + "_value_textbox").kendoNumericTextBox({ format: format });
            }

            function walletDropDownEditor(container, options) {
                rowId = kendo.guid();
                $('<input required name="' + options.field + '" data-bind="value:'+ options.field+'"/>')
                    .appendTo(container)
                    .kendoDropDownList({
                        autoBind: false,
                        dataTextField: "Name",
                        dataValueField: "WalletId",
                        change: function(e) {
                            templateDefalt = this.dataItem();
                            walletDropDownChange();
                        },
                        dataSource: {
                            data: templates
                        }
                    });
            }
            function calcDropDownEditor(container, options) {
                $('<input required name="' + options.field + '" data-bind="value:'+ options.field+'"/>')
                    .appendTo(container)
                    .kendoDropDownList({
                        autoBind: false,
                        dataTextField: "name",
                        dataValueField: "CalcType",
                        change: function(e) {
                            calcDefalt = this.dataItem();
                            calcDropDownChange();
                        },
                        dataSource: {
                            data: calcTypes
                        }
                    });
            }
            function autoDepositEditor(container, options) { 
                $('<input id="' + rowId + '_auto_deposit_checkbox" type="checkbox" name="AutoDeposit" data-type="boolean" data-bind="checked:AutoDeposit">').appendTo(container); 
            }
            function valueEditor(container, options) {
                var id = rowId + '_' + options.field.toLowerCase() + '_textbox';
                $('<input type="number"  name="' + options.field + '"  id="' + id+'" data-bind="value:'+ options.field+'" style="width:20%" />')
                .appendTo(container).kendoNumericTextBox({
                    min: 0,
                    max: 100,
                    step: 10
                    });
            }

              @*$.ajax({
                                url:  '@Html.Raw(Url.Action("UpdateWallet", "Product"))',
                                dataType: "json",
                                method: "POST",
                                data: options.data.models,
                                success: function (data) {
                                    if (data.result != true) {
                                       options.error(data.message);
                                    }
                                    options.success(data);
                                },
                                error: function(result) {
                                  // notify the data source that the request failed
                                  options.error(result);
                                }
                              });*@

            $("#product-wallet-grid").kendoGrid({
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: '@Html.Raw(Url.Action("Wallets", "Product"))',
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken({
                                "productId": productId
                            })
                        },
                        create:{
                            url: '@Html.Raw(Url.Action("UpdateWallet", "Product"))',
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken({
                                "productId": productId
                            })
                        },
                        update: {
                            url: '@Html.Raw(Url.Action("UpdateWallet", "Product"))',
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken({
                                "productId": productId
                            })
                        },
                        destroy: {
                            url: '@Html.Raw(Url.Action("DeleteWallet", "Product"))',
                            type: "POST",
                            dataType: "json",
                            data: addAntiForgeryToken({
                                "productId": productId
                            })
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors",
                        model: {
                            id: "Id",
                            fields: {
                                Name: {
                                    editable: false,
                                    type: "string"
                                },
                                CalcType: {
                                    editable: true,
                                    type: "number",
                                    defaultValue: calcDefalt.CalcType
                                },
                                Value: {
                                    editable: true,
                                    type: "number",
                                    defaultValue:100
                                },
                                MaxValue: {
                                    editable: true,
                                    type: "number",
                                    defaultValue:50
                                },
                                AutoDeposit: {
                                    editable: true,
                                    type: "boolean",
                                    defaultValue:templateDefalt.CanDeposit
                                },
                                Id: {
                                    editable: false,
                                    type: "number"
                                },
                                WalletId: {
                                    editable: true,
                                    type: "number",
                                    defaultValue: templateDefalt.WalletId
                                }
                            }
                        }
                    },
                    requestEnd: function (e) { 
                        if (e.type == "create" || e.type == "update") {
                            if (e.response.result == true) {
                                this.read();
                            } else {
                                this.options.error.call(this, e.response);
                            }
                        } 
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                },

                toolbar: [{
                    name: "create",
                    text: '@T("Admin.Common.AddNewRecord")'
                }],
                editable: {
                    confirmation: '@T("Admin.Common.DeleteConfirmation ")',
                    mode: "inline"
                },
                scrollable: false,
                columns: [{
                    field: "WalletId",
                    title: '@T("Admin.Catalog.Products.Wallet.Name")',
                    width: 100,
                    editor: walletDropDownEditor,
                    template: "#=Name#"
                },
                {
                    field: "CalcType",
                    title: '@T("Admin.Catalog.Products.Wallet.CalcType")',
                    width: 100,
                    editor: calcDropDownEditor, 
                    template: function(dataItem) { 
                       return  kendo.htmlEncode(calcTypes.filter((item)=>{return item.CalcType==dataItem.CalcType;})[0].name)  ;
                    }
                },
                {
                    field: "Value",
                    title: '@T("Admin.Catalog.Products.Wallet.Value")',
                    width: 100,
                    editor: valueEditor,
                    template: function(dataItem) {
                       var prefix=dataItem.CalcType==1?"%":"";
                       return   kendo.htmlEncode(dataItem.Value+prefix) ;
                    }
                },
                {
                    field: "MaxValue",
                    title: '@T("Admin.Catalog.Products.Wallet.MaxValue")',
                    width: 100,
                    editor: valueEditor,
                    template: function(dataItem) {
                        var prefix=dataItem.CalcType==1?"%":"";
                       return   kendo.htmlEncode(dataItem.MaxValue+prefix) ;
                    }
                },
                {
                    field: "AutoDeposit",
                    title: '@T("Admin.Catalog.Products.Wallet.AutoDeposit")',
                    //integer format
                    width: 100,
                    editor: autoDepositEditor,
                    template: function(dataItem) {
                        var value=dataItem.AutoDeposit==true?"是":"否";
                       return   kendo.htmlEncode(value) ;
                    }
                },
                {
                    command: [{
                        name: "edit",
                        text: {
                            edit: '@T("Admin.Common.Edit")',
                            update: '@T("Admin.Common.Update")',
                            cancel: '@T("Admin.Common.Cancel")'
                        }
                    },
                    {
                        name: "destroy",
                        text: '@T("Admin.Common.Delete")'
                    }],
                    width: 200
                }],
                edit: function (e) {
                    walletDropDownChange();
                    calcDropDownChange();
                }
            });
        },
        error: function (xhr, ajaxOptions, thrownError) {
            toastr.error('钱包数据加载失败');
        }
    });
});
</script>
<div class="panel-group">
    @if (Model.Id > 0)
    {
        <div class="panel panel-default">
            <div class="panel-heading">
                @T("Admin.Catalog.Products.Wallet.AddNew")
            </div>
            <div class="panel-body">
                <div id="product-wallet-grid"></div>


            </div>
        </div>
    }
    else
    {
        <div class="panel panel-default">
            <div class="panel-body">
                @T("Admin.Catalog.Products.Wallet.SaveBeforeEdit")
            </div>

        </div>
    }
</div>